package com.yugao.fintech.antelope.infra;

import com.alibaba.cloud.nacos.NacosServiceAutoConfiguration;
import com.yugao.fintech.antelope.infra.common.ScanScene;
import com.yugao.fintech.antelope.infra.module.file.constants.FileCons;
import com.yugao.fintech.antelope.infra.module.file.model.rqrs.FileRecordResp;
import com.yugao.fintech.antelope.infra.module.file.model.rqrs.MediaMaterialResp;
import com.yugao.fintech.antelope.infra.module.group.model.rqrs.DataGroupListReq;
import com.yugao.fintech.antelope.infra.module.group.model.rqrs.DataGroupResp;
import com.yugao.fintech.antelope.infra.module.group.model.rqrs.DataGroupSaveReq;
import com.yugao.fintech.antelope.infra.module.group.model.rqrs.DataGroupUpdateReq;
import com.yugao.fintech.antelope.base.model.enums.DataGroupBizTypeEnum;
import com.yugao.fintech.framework.assistant.utils.UrlUtils;
import com.yugao.fintech.framework.assistant.utils.exception.BizException;
import com.yugao.fintech.framework.core.response.Resp;
import com.yugao.fintech.framework.core.response.RetOps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;


@Lazy
@Slf4j
@Service
@ConditionalOnClass({FeignAutoConfiguration.class, NacosServiceAutoConfiguration.class})
public class InfraCloudApiImpl implements InfraApi {
    @Autowired
    private InfraRemote infraRemote;

    @Override
    public MediaMaterialResp getMediaMaterial(Long id) {
        Resp<MediaMaterialResp> resp = infraRemote.getMediaMaterial(id);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public Long saveDataGroup(DataGroupSaveReq req) {
        Resp<Long> resp = infraRemote.saveDataGroup(req);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public List<DataGroupResp> listChildrenDataGroup(Long parentId) {
        Resp<List<DataGroupResp>> resp = infraRemote.listChildrenDataGroup(parentId);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public List<DataGroupResp> listDataGroup(DataGroupListReq req) {
        Resp<List<DataGroupResp>> resp = infraRemote.listDataGroup(req);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public boolean updateDataGroup(DataGroupUpdateReq req) {
        Resp<Boolean> resp = infraRemote.updateDataGroup(req);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public DataGroupResp getDataGroup(Long id) {
        Resp<DataGroupResp> resp = infraRemote.getDataGroup(id);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public boolean deleteDataGroup(DataGroupBizTypeEnum type, Long id) {
        Resp<Boolean> resp = infraRemote.deleteDataGroup(type.getType(), id);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public boolean batchDeleteDataGroup(DataGroupBizTypeEnum type, List<Long> ids) {
        Resp<Boolean> resp = infraRemote.batchDeleteDataGroup(type.getType(), ids);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public String getFileIntranetUrl(String filePath) {
        filePath = filePath.replace(FileCons.UPLOAD_BASE_URL, "");
        Resp<String> resp = infraRemote.getFileIntranetUrl();
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        String url = UrlUtils.removeRepeatSlashOfUrl(resp.getData() + "/" + filePath);
        log.debug("intranet file url: {}", url);
        return url;
    }

    @Override
    public FileRecordResp getFileRecordByPath(String path, boolean isIntranet) {
        Resp<FileRecordResp> resp = infraRemote.getFileRecordByPath(path, isIntranet);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public void deleteFileRecordByPath(String path) {
        Resp<?> resp = infraRemote.deleteFileRecordByPath(path);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
    }

    @Override
    public String getMpQrcode(ScanScene req) {
        Resp<String> resp = infraRemote.getMpQrcode(req);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public Short getIdGenNextWorkId() {
        Resp<Short> resp = infraRemote.getIdGenNextWorkId();
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public void refreshIdGenWorkId(Short workId) {
        Resp<Boolean> resp = infraRemote.refreshIdGenWorkId(workId);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
    }
}
